//
//  ACButton.m
//  ACFramework
//
//  Created by caoyq on 2017/9/11.
//

#import "ACButton.h"

@implementation ACButton

- (instancetype)initWithFrame:(CGRect)frame Layout: (ACButtonLayout)layout padding: (CGFloat)padding {
    self = [super initWithFrame:frame];
    if (self) {
        _buttonLayout = layout;
        _padding = padding;
    }
    return self;
}

- (instancetype)initWithLayout: (ACButtonLayout)layout padding: (CGFloat)padding {
    self = [super init];
    if (self) {
        _buttonLayout = layout;
        _padding = padding;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGSize titleSize = self.titleLabel.bounds.size;
    CGSize imageSize = self.currentImage.size;
    CGFloat imageTopEdge = 0;
    CGFloat imageLeftEdge = 0;
    CGFloat imageBottomEdge = 0;
    CGFloat imageRightEdge = 0;
    CGFloat titleTopEdge = 0;
    CGFloat titleLeftEdge = 0;
    CGFloat titleBottomEdge = 0;
    CGFloat titleRightEdge = 0;
    switch (_buttonLayout) {
        case ACButtonLayoutImageTop:
        {
            //图片：右移动标题宽度，上移动标题高度(图片会存在超出button范围可能，文字则不会)
            imageTopEdge = -(titleSize.height + _padding);
            imageRightEdge = -titleSize.width;
            
            titleLeftEdge = -imageSize.width;
            titleBottomEdge = -(imageSize.height + _padding);
        }
            break;
        case ACButtonLayoutImageLeft:
        {
            imageLeftEdge = -_padding;
            titleLeftEdge = _padding;
        }
            break;
        case ACButtonLayoutImageBottom:
        {
            titleTopEdge = -(imageSize.height + _padding);
            titleLeftEdge = -imageSize.width;
            
            imageBottomEdge = -(titleSize.height + _padding);
            imageRightEdge = -titleSize.width;
        }
            break;
        case ACButtonLayoutImageRight:
        {
            imageLeftEdge = titleSize.width + _padding / 2;
            imageRightEdge = -imageLeftEdge;
            titleLeftEdge = -(imageSize.width + _padding / 2);
            titleRightEdge = -titleLeftEdge;
        }
            break;
    }
    self.titleEdgeInsets = UIEdgeInsetsMake(titleTopEdge, titleLeftEdge, titleBottomEdge, titleRightEdge);
    self.imageEdgeInsets = UIEdgeInsetsMake(imageTopEdge, imageLeftEdge, imageBottomEdge, imageRightEdge);
}

@end
